/**
 * Copyright (c) 2015-2019, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.service;

import cn.hg.jfsa.model.JfsaDict;
import cn.hg.jfsa.web.theme.ThemeFile;
import cn.hg.jfsa.web.theme.ThemeManager;
import com.jfinal.aop.Inject;
import com.jfinal.kit.FileKit;
import com.jfinal.kit.StrKit;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Golly
 *
 */
public class JfsaThemeService {

	@Inject
	private JfsaDictService dictService;

	private static final String DICT_PARENT_KEY = "edit_theme_file_type";

	private List<JfsaDict> dictList;

	/**
	 * 取得当前主题某文件夹下的文件列表
	 *
	 * @param dir   某文件夹名
	 * @param parent 是否定位到上级文件夹
	 * @return 文件列表
	 */
	public List<ThemeFile> getFileList(String dir) {
		List<ThemeFile> themeFiles = new ArrayList<>();
		if (StrKit.isBlank(dir)) {
			dir = "";
		}

		File pathFile = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + dir);

		if (!pathFile.isDirectory()) {
			return themeFiles;
		}

		File[] files = pathFile.listFiles();

		for (File f : files) {
			// 依据字典中定义的可编辑文件后缀过滤文件
			if (f.isDirectory() || isInDict(f)) {
				ThemeFile tf = new ThemeFile();
				tf.setAbsolutePath(f.getAbsolutePath());
				tf.setDir(f.isDirectory());
				tf.setExt(FileKit.getFileExtension(f));
				tf.setName(f.getName());
				tf.setFileCount(f.isDirectory() ? f.listFiles().length : 0);
				themeFiles.add(tf);
			}
		}

		return themeFiles;
	}

	/**
	 * 判断文件的后缀是否存在于字典列表中
	 *
	 * @param f 文件
	 * @return true = 存在，false = 不存在
	 */
	private boolean isInDict(File f) {
		if (dictList == null) {
			dictList = dictService.getByParentKey(DICT_PARENT_KEY, "on");
		}

		for (JfsaDict dict : dictList) {
			if (FileKit.getFileExtension(f).equalsIgnoreCase(dict.getKey())) {
				return true;
			}
		}

		return false;
	}

	public String getFileContent(String path, String file) throws IOException {
		File f = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + path + "/" + file);

		return FileUtils.readFileToString(f, "utf-8");
	}

	public String getFileEditMode(String file) {
		String mode = "ace/mode/text";
		if (dictList == null) {
			dictList = dictService.getByParentKey(DICT_PARENT_KEY, "on");
		}

		for (JfsaDict dict : dictList) {
			if (FileKit.getFileExtension(file).equalsIgnoreCase(dict.getKey())) {
				return dict.getPara();
			}
		}

		return mode;
	}

	public void updateFileContent(String dir, String file, String fileContent) throws IOException {
		File f = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + dir + "/" + file);
		FileUtils.write(f, fileContent, "utf-8");
	}

	/**
	 * 创建文件
	 *
	 * @param dir
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public String newFile(String dir, String file) throws IOException {
		File f = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + dir + "/" + file);

		if (!isInDict(f)) {
			return "文件后缀不符合要求！";
		}

		if (f.exists()) {
			return "文件已经存在！";
		}

		f.createNewFile();

		return null;
	}

	public boolean delFile(String dir, String file) {
		File f = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + dir + "/" + file);

		if (f.exists()) {
			return f.delete();
		}

		return false;
	}

	public String newDir(String dir, String file) {
		File f = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + dir + "/" + file);

		if (f.exists()) {
			return "文件夹已经存在！";
		}

		f.mkdir();

		return null;
	}

	public String delDir(String dir, String file) {
		File f = new File(ThemeManager.me().getCurrentTheme().getAbsolutePath() + dir + "/" + file);

		if (!f.exists()) {
			return "文件夹不存在！";
		}

		if (!f.isDirectory()) {
			return "目标不是文件夹！";
		}

		if (f.listFiles().length > 0) {
			return "文件夹非空，不允许删除！";
		}

		f.delete();

		return null;
	}
}
